package com.miage.ldapsec.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.miage.ldapsec.service.LDAPService;

@Controller
@RequestMapping("/home/*")
public class HomeController {

	@Autowired
    private LDAPService ldap;
	
	@RequestMapping(value = "index")
	public void index(Model model) {
		String login = SecurityContextHolder.getContext().getAuthentication().getName();
		String role = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toString();

		if(role.equals("[ROLE_ADMIN]")){
	        role = "qu'<em>Administrateur</em>";
	    } else if(role.equals("[ROLE_HELPDESK]")){
	    	role = "que <em>Helpdesk</em>";
        } else if(role.equals("[ROLE_MANAGER]")){
        	role = "que <em>Group manager</em>";
        } else if(role.equals("[ROLE_ANONYMOUS]")){
        	role = "qu'anonyme";
        } else if(role.equals("[]")){
            role = "que simple utilisateur";
            model.addAttribute("simpleUser", "yes");
        }
		
		model.addAttribute("user", ldap.getUser(login).getLogin());
		model.addAttribute("role", role);
		model.addAttribute("nbUsers", ldap.nbUsers());
		model.addAttribute("nbGroups", ldap.nbUserGroups());
		model.addAttribute("nbAdmin", ldap.getRoleGroup("admin").nbMember());
		model.addAttribute("nbHelpdesk", ldap.getRoleGroup("helpdesk").nbMember());
		model.addAttribute("nbManager", ldap.getRoleGroup("manager").nbMember());

	}
	
	@RequestMapping(value = "credits")
    public void credits(Model model) {
	    model.addAttribute("isCredits", "yes");
    }
}

